import mongoose from 'mongoose';
import { formatDateByNow } from '~/utils/util.date';

export interface IResource extends mongoose.Document {
	icon: string;
	name: string;
	description: string;
	url: string;
	details: string;
	mainCategoryId: mongoose.Schema.Types.ObjectId;
	subCategoryId: mongoose.Schema.Types.ObjectId;
	isRecommend: boolean;
	createdAt?: string;
	updatedAt?: string;
}

// 资源
const ResourceSchema = new mongoose.Schema<IResource>(
	{
		icon: {
			type: String,
		},
		name: {
			type: String,
			required: true,
		},
		description: {
			type: String,
			required: true,
		},
		url: {
			type: String,
			required: true,
			unique: true,
		},
		details: {
			type: String,
		},
		mainCategoryId: {
			type: mongoose.Schema.Types.ObjectId,
			required: true,
		},
		subCategoryId: {
			type: mongoose.Schema.Types.ObjectId,
			required: true,
		},
		isRecommend: {
			type: Boolean,
			default: false,
		},
		createdAt: {
			type: String,
			default: formatDateByNow(),
		},
		updatedAt: {
			type: String,
			default: formatDateByNow(),
		},
	},
	{
		collection: 'resources',
	}
);

export const ResourceModel = mongoose.model('Resource', ResourceSchema);
